package com.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author tiny
 * @date 2021/9/9 21:57
 * @Description:
 */
public class DateUtil {

    /**
     * 计算两个date间的相隔天数(不到一天的算一天)
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int getDayDiffer(Date startDate, Date endDate) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime();
        long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime();
        int days = (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24)) ;
        return (endDateTime - startDateTime) % (1000 * 3600 * 24) > 0 ? days+1 : days;
    }

    /**
     * 获得指定日期的后一天
     * @param date
     * @return
     */
    public static String getDayAfter(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);
        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }

}
